var $ = require('../../utils/Ajax.js');
Page({
  data: {
    Search: [],
    SearchCont: '',
    Experience: null,
    // 图片地址
    PictureUrl: getApp().PictureUrl,
    SearchText: 1
  },

  onLoad: function (options) {
    this.setData({
      SearchCont: options.SearchCont
    })

    var that = this;
    $.Ajax("api/x_list_exp", "GET", {
      kw: options.SearchCont
    }, function (res) {
      console.log(res)
      that.setData({
        Experience: res.d.list_experience.data
      })
    });
  },

  onShow: function(){
    var Search = wx.getStorageSync('Search');
    if (Search.length != 0) {
      this.setData({
        Search: Search
      })
    }
  },

  // 监听输入框
  CompleteSearch: function(e){
    var Search = e.detail.value.replace(/\s+/g, '');
    if (Search.length > 0) {
      this.setData({
        SearchCont: Search,
        SearchText: 1
      })
    } else {
      this.setData({
        SearchCont: Search,
        SearchText: 0
      })
    }
  },

  // 点击取消按钮回到首页
  CloseSearch:function(){
    wx.navigateBack({
      delta: 2
    })
  },

  // 点击进入详情
  ExperDetails: function (e) {
    var Id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../ExperDetails/ExperDetails?Id=' + Id,
    })
  },

  // 点击搜索
  ClickSearch: function(){
    if (this.data.SearchCont == ''){
      wx.showToast({
        title: '搜索内容不能为空',
        icon: 'none',
        duration: 1500
      })
    } else {
      var that = this;
      var Search = this.data.Search;
      // 判断搜索记录是否大于十个
      // 如果为真 则取前九个
      if (Search.length >= 10) {
        Search = Search.slice(0,9);
      }
      var SearchCont = this.data.SearchCont;
      Search.unshift(SearchCont);
      this.setData({
        Search: Search
      })
      wx.setStorageSync('Search', this.data.Search);
      $.Ajax("api/x_list_exp", "GET", {
        kw: that.data.SearchCont
      }, function (res) {
        that.setData({
          Experience: res.d.list_experience.data
        })
      });
    }
  }
})